package collect.taobao.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import collect.taobao.bean.ImageStreamBean;
import collect.taobao.service.ImageStreamService;
import collect.taobao.util.SuperAction;
import collect.taobao.util.groupPage.Pagination;
import collect.taobao.util.groupPage.PageBuffer;

public class ImageStreamAction extends SuperAction {
	List list = new ArrayList();
	public String emailUrl ;

	public ImageStreamAction() {
		super();
	}

	public String showGallery() throws IOException, ServletException {
		HttpServletRequest request = this.getReq();
		HttpServletResponse response = this.getResp();
		
		response.addHeader("Content-Type", "image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		String pageNo = request.getParameter("PageNo");
		int currentPageNo = 0;
		if (pageNo == null || "".equals(pageNo)) {
			currentPageNo = 1;
		} else {
			currentPageNo = Integer.parseInt(pageNo);
		}
		String sql = "select * from emailtable.imagestream  ORDER BY  NAME ASC ";
		StringBuffer url = new StringBuffer();
		url.append("showGallery!showGallery.action");
	
		PageBuffer pg = Pagination.pageContent(currentPageNo, sql, url.toString(),1);
		List list = pg.getList();
		request.setAttribute("pagination", pg.getNavigator1());
	
		// String path = request.getParameter("path");
		if (list != null && list.size() > 0) {
				Object[] obj = (Object[]) list.get(0);
				emailUrl = (String) obj[2];
		}
		request.setAttribute("imageUrl", emailUrl);

		return "success";
	}
	
	public void imageView() throws IOException {
		HttpServletRequest request = this.getReq();
		String path = request.getParameter("url");
		HttpServletResponse response = this.getResp();
		response.addHeader("Content-Type", "image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		ServletOutputStream out = null ;
		try {
			BufferedImage img = null;
			img = ImageIO.read(new File(path));
			out = response.getOutputStream();
			ImageIO.write(img, "jpeg", out);
			out.flush();
			out.close();
		} catch (Exception e) {
		} finally {
			if (out != null)
				out.close();
		}
	}

	//保存图片到数据库,只保存路径和文件名。
	public String imageSave() {
		ServletContext servletContext = this.getReq().getSession()
				.getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		ImageStreamService logicServer = (ImageStreamService) ctx
				.getBean("imageStreamServerInterface");
		File file = new File("D:/manhua/sishen/diyi");
		List<ImageStreamBean> list = fileList(file);
		int success = 0;
		for (int i = 0; i < list.size(); i++) {
			ImageStreamBean bean = (ImageStreamBean) list.get(i);
			success = logicServer.save(bean);
			if (success == 1) {
				System.out.println("����ɹ�");
			}
		}
		return "success";
	}

	public List fileList(File file) {
		ImageStreamBean bean = new ImageStreamBean();
		if (file.isDirectory()) {
			File files[] = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				fileList(files[i]);
			}
		} else {
			String fileName = file.getName();
			String filePath = file.getPath();
			bean.setName(fileName);
			bean.setPath(filePath);
			list.add(bean);
		}
		return list;
	}

	public String getEmailUrl() {
		return emailUrl;
	}

	public void setEmailUrl(String emailUrl) {
		this.emailUrl = emailUrl;
	}

}
